package com.powerManager.sdk.handlers;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;

import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;
import com.powerManager.sdk.utils.JsonUtil;

public class RuleHandler {

	public String loadJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql="select rule_id,name,is_public isPublic,remark from sys_rule";
		String page = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		PagingResult pr = new PagingResult(sql);
		String json = null;
		try {
			json = pr.pagingToJson(Integer.parseInt(pageSize), Integer.parseInt(page));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(json);
		return "text:"+json;
	}
	
	public String remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "delete from sys_rule where rule_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object args = request.getParameter("ID");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "update sys_rule set name=?,is_public=?,remark=?,rule_data=?,objectName=? where rule_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[6];
		args[0] = request.getParameter("displayName");
		args[1] = request.getParameter("isPublic");
		args[2] = request.getParameter("remark");
		args[3] = request.getParameter("DataPrivilegeRule");
		args[4] = request.getParameter("DataPrivilegeView");
		args[5] = request.getParameter("DataPrivilegeID");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "insert into sys_rule(rule_id,name,is_Public,remark,status,rule_data,objectName) values(GET_SEQ('SEQ_PRIVILEGE_ID','0'),?,?,?,1,?,?)";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[5];
		args[0] = request.getParameter("displayName");
		args[1] = request.getParameter("isPublic");
		args[2] = request.getParameter("remark");
		args[3] = request.getParameter("DataPrivilegeRule");
		args[4] = request.getParameter("DataPrivilegeView");
		jdbc.update(sql, args);
		return "text:true";
	}
	
	public String getDataPrivilege(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql="select rule_id DataPrivilegeID,name displayName,is_public isPublic,remark,objectName DataPrivilegeView,rule_data DataPrivilegeRule from sys_rule where rule_id=?";
		String id = request.getParameter("ID");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		Map<String, Object > jsonRule = new HashMap<String, Object>();
		try {
			Map<String, Object > rule = jdbc.queryForMap(sql, id);
			jsonRule.put("Data", rule);
			jsonRule.put("IsError", false);
			jsonRule.put("Message", "加载成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			jsonRule.put("IsError", true);
			jsonRule.put("Message", "加载失败!");
			e.printStackTrace();
		}
		return "text:"+JsonUtil.toJson(jsonRule);
	}
	
	public String getSettingsJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql="select obj_id id,table_name name,show_name display,'0' parentID,'string' type " +
				"from sys_obj_definition UNION " +
				"select col_id id,COLUMN_name name,show_name display,obj_id parentID,'string' type " +
				"from sys_obj_column";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> json = null;
		try {
			json = pr.noPagingTree(sql, null, "id", "parentID", "0");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(json);
		return "text:"+JsonUtil.toJson(json);
	}
	
	public String selectUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select user_id as id,user_id as value,nickname as text from sys_user";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> json = null;
		try {
			json = pr.noPaging(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(json);
		return "text:"+JsonUtil.toJson(json);
	}
	
	public String selectRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select role_id as id,role_id as value,role_name as text from sys_role";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> json = null;
		try {
			json = pr.noPaging(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(json);
		return "text:"+JsonUtil.toJson(json);
	}
	
	public String selectDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select department_id as id,department_id as value,department_name as text from sys_department";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> json = null;
		try {
			json = pr.noPaging(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(json);
		return "text:"+JsonUtil.toJson(json);
	}
}
